﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblActiviteDAL:ItblActivite
    {
        HKEA_CMSEntities model;
        tblActivite activite;

        public tblActiviteDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblActiviteDAL GetInstance()
        {
            return new tblActiviteDAL();
        }

        public int InsertENT(tblActivite ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblActivites.AddObject(ent);
            return model.SaveChanges();
        }

        public List<tblActivite> GetListByENT(tblActivite ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblActivites.Where(a => a.ActiviteUID > 0);
            if (ent.ActiviteUID > 0)
                result = result.Where(a => a.ActiviteUID == ent.ActiviteUID);
            if (ent.ActiviteTypeUID > 0)
                result = result.Where(a=>a.ActiviteTypeUID==ent.ActiviteTypeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public int SaveList(List<tblActivite> ToAdd, List<tblActivite> ToUpdate, List<int> ToDel, string LastModifyBy)
        {
            foreach (var temp in ToAdd)
            {
                if (temp != null)
                {
                    var ent = new tblActivite();
                    ent.LastModifyDate = DateTime.Now;
                    ent.LastModifyBy = LastModifyBy;
                    ent.CreationDate = DateTime.Now;
                    ent.StartDateTime = DateTime.Now;
                    ent.EndDateTime = DateTime.Now;
                    ent.EarlyBirdEndDateTime = null;
                    ent.EarlyBirdStartDateTime = null;
                    ent.Activite_Name = temp.Activite_Name;
                    ent.Activite_Theme = temp.Activite_Theme;
                    ent.Activitecode = temp.Activitecode;
                    ent.ActiviteTypeUID = temp.ActiviteTypeUID;
                    ent.StartDateTime = temp.StartDateTime;
                    ent.EndDateTime = temp.EndDateTime;
                    model.tblActivites.AddObject(ent);
                }
            }
            foreach (var temp in ToUpdate)
            {
                var result = model.tblActivites.Where(a => a.Activitecode == temp.Activitecode).FirstOrDefault();
                if (result != null)
                {
                    result.LastModifyDate = DateTime.Now;
                    result.LastModifyBy = LastModifyBy;
                    result.Activite_Name = temp.Activite_Name;
                    result.Activite_Theme = temp.Activite_Theme;
                    result.Activitecode = temp.Activitecode;
                    result.StartDateTime = DateTime.Now;
                    result.EndDateTime = DateTime.Now;
                    result.EarlyBirdEndDateTime = null;
                    result.EarlyBirdStartDateTime = null;
                    result.Activite_Name = temp.Activite_Name;
                    result.Activite_Theme = temp.Activite_Theme;
                    result.Activitecode = temp.Activitecode;
                    result.ActiviteTypeUID = temp.ActiviteTypeUID;
                    result.StartDateTime = temp.StartDateTime;
                    result.EndDateTime = temp.EndDateTime;
                }
            }
            foreach (var temp in ToDel)
            {
                var result = model.tblActivites.Where(a => a.ActiviteUID == temp).FirstOrDefault();
                {
                    if (result != null)
                        model.tblActivites.DeleteObject(result);
                }
            }
            return model.SaveChanges();
        }

        public List<tblActivite> GetListByENT(tblActivite ent)
        {
            var result = model.tblActivites.Where(a => a.ActiviteUID > 0);
            if (ent.ActiviteUID > 0)
                result = result.Where(a => a.ActiviteUID == ent.ActiviteUID);
            if (ent.ActiviteTypeUID > 0)
                result = result.Where(a => a.ActiviteTypeUID == ent.ActiviteTypeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            if (!string.IsNullOrEmpty(ent.Activitecode))
                result = result.Where(a=>a.Activitecode==ent.Activitecode);
            return result.OrderByDescending(a=>a.ActiviteUID).ToList();
            
        }

        public int GetCountByENT(tblActivite ent)
        {
            var result = model.tblActivites.Where(a => a.ActiviteUID > 0);
            if (ent.ActiviteUID > 0)
                result = result.Where(a => a.ActiviteUID == ent.ActiviteUID);
            if (ent.ActiviteTypeUID > 0)
                result = result.Where(a => a.ActiviteTypeUID == ent.ActiviteTypeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblActivite ent)
        {
            activite = model.tblActivites.Where(a=>a.ActiviteUID==ent.ActiviteUID).FirstOrDefault();
            if (activite != null)
            {
                activite.LastModifyBy = ent.LastModifyBy;
                activite.LastModifyDate = DateTime.Now;
                activite.Activite_Name = ent.Activite_Name;
                activite.Activite_Theme = ent.Activite_Theme;
                activite.ActiviteTypeUID = ent.ActiviteTypeUID;
                activite.EarlyBirdEndDateTime = ent.EarlyBirdEndDateTime;
                activite.EarlyBirdStartDateTime = ent.EarlyBirdStartDateTime;
                activite.EndDateTime = ent.EndDateTime;
                activite.MemberCharge = ent.MemberCharge;
                activite.MemberEarlyBirdCharge = ent.MemberEarlyBirdCharge;
                activite.NonMemberCharge = ent.NonMemberCharge;
                activite.NonMemberEarlyBirdCharge = ent.NonMemberEarlyBirdCharge;
                activite.NoOfSeat = ent.NoOfSeat;
                activite.Remarks = ent.Remarks;
                activite.Presenters = ent.Presenters;
                activite.StartDateTime = ent.StartDateTime;
                activite.Venue = ent.Venue;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblActivite ent)
        {
            var temp = model.tblActivites.Where(a => a.ActiviteUID > 0);
            if (ent.ActiviteUID > 0)
                temp = temp.Where(a=>a.ActiviteUID==ent.ActiviteUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblActivites.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
